grErp.controller('LossCtrl',['$scope','$filter','LossFactory','inventoryFactory',function($scope,$filter,LossFactory,inventoryFactory) {
    $scope.items=[{}];
    setTimeout(function(){
        $(".time").datetimepicker(
            {
                format: 'Y-m-d H:i:s',
                todayButton: true,
                step:20,
            }
        )
    },500);
    $scope.moreItems=function(){
        $scope.items.push({});
    }
    $scope.removeItem=function(index){
        if(confirm("确定删除该条")){
            $scope.items.splice(index,1)
        }
    }
    $scope.lossinit=function(){
        loadingStart();
        LossFactory.ajaxgetinventory().then(function(d){
            loadingEnd();
            $scope.productList=d;
        });
        LossFactory.ajaxGetLossReason().then(function(d){
            $scope.lossReasonList=d;
        })

    }
    $scope.lossProductSelect=function(itemIndex,proIndex){
        var product=$scope.productList[proIndex];
        var BIN=product.BIN;
        $scope.items[itemIndex].BIN=BIN;
        $scope.items[itemIndex].unit=$filter('getSpecUnit')(product.spec);
        console.log($scope.items[itemIndex].unit);
        inventoryFactory.getybzlist(BIN).then(function(d){
            $scope.items[itemIndex].batchList=d;
        });
    }
    $scope.batchSelect=function(itemIndex,batchIndex){

    }
}])
grErp.factory('LossFactory',['$http','$q',function($http,$q){
    var service={};
    service.ajaxgetinventory = function(type){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Admin/Inventory/ajaxgetinventory",
            params:{type:type}
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.ajaxGetLossReason = function(){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Admin/Inventory/ajaxGetLossReason",
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    };
    return service;
}])
